<template>
	<view class="content" :style="'background-image:url('+detail.cover+')'" v-if="detail">
		<view class="info-card">
			<view class="text">
				{{getContent(detail.content)}}
			</view>
			<view class="date">
				<uni-dateformat :date="detail.create_time" />
			</view>
			<view class="icon" :class="playStatus?'active':''" @click="play">
				<u-icon :name="playStatus?'pause-circle':'play-circle'" color="white" size="24"></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	const innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = true;
	const db = uniCloud.database();
	const dbCollectionName = 'daily-english';
	export default {
		data() {
			return {
				playStatus: false,
				detail: null
			}
		},
		onLoad(e) {
			this._id = e.id
			this.getDetail(e.id)
		},
		computed: {
			getContent() {
				return (text) => {
					return text.replace(/\\n/g, '\n')
				}
			},

		},
		methods: {
			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field("state,text,cover,content,audio,create_time").get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.detail = data
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},
			/**
			 * @param {Object} url 播放音频
			 */
			play() {
				if (this.playStatus) {
					innerAudioContext.pause()
					this.playStatus = false
					return
				}else{
					innerAudioContext.src = this.detail.audio
					innerAudioContext.play()
				}
				
				innerAudioContext.onPlay(() => {
					this.playStatus = true
				});
				innerAudioContext.onPause(() => {
					this.playStatus = false
				});
				innerAudioContext.onEnded(() => {
					this.playStatus = false
				})
				innerAudioContext.onError(() => {
					this.playStatus = false
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
		background-position: center center;
		background-size: cover;


		.info-card {
			overflow: hidden;
			backdrop-filter: blur(20rpx);
			margin-top: auto;
			margin-bottom: 40rpx;
			margin-left: 40rpx;
			margin-right: 40rpx;
			padding: 30rpx;
			color: white;

			// background-color: rgba(255, 255, 255, 0.1);
			background-image: linear-gradient(-225deg, rgba(255, 254, 255, 0.5) 0%, rgba(215, 255, 254, 0.5) 100%);
			display: flex;
			flex-direction: column;

			.text {
				white-space: pre-wrap;
				font-size: 32rpx;
			}

			.date {

				margin-top: 10rpx;
				font-size: 20rpx;
			}

			.icon {
				align-self: flex-end;
				background-image: linear-gradient(to right, #fddb92 0%, #d1fdff 100%);
				border-radius: 50%;
				width: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				height: 80rpx;
			}
		}

	}

	@-webkit-keyframes changeright {

		0% {
			-webkit-transform: rotate(0deg);
		}

		50% {
			-webkit-transform: rotate(180deg);
		}

		100% {
			-webkit-transform: rotate(350deg);
		}
	}

	.active {
		background-image: linear-gradient(to right, #a8edea 0%, #fed6e3 100%) !important;
		-webkit-animation: changeright 6s linear infinite;
	}
</style>
